package cn.xlunzi.games.activity

import android.app.Activity
import android.os.Bundle
import android.os.Vibrator
import cn.xlunzi.games.R
import cn.xlunzi.games.common.Constant.DEFAULT_NUM
import cn.xlunzi.games.common.Constant.IS_LEI_NUM
import cn.xlunzi.games.common.Constant.TYPE_HIDE
import cn.xlunzi.games.common.Constant.TYPE_SHOW
import cn.xlunzi.games.common.Constant.TYPE_SIGN
import cn.xlunzi.games.manger.LeiMgr
import cn.xlunzi.games.manger.LeiMgr.COLUMN
import cn.xlunzi.games.manger.LeiMgr.ROW
import cn.xlunzi.games.view.LeiView
import kotlinx.android.synthetic.main.activity_saolei.*
import java.text.SimpleDateFormat
import java.util.*


class SaoLeiActivity : Activity() {

    private var leiNum: Int = LeiMgr.TOTAL

    private val numList = MutableList(COLUMN * ROW, { DEFAULT_NUM })
    private val typeList = MutableList(COLUMN * ROW, { TYPE_HIDE })

    private var stop = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_saolei)

        resetGame()
        lei_view.initLei(numList, typeList, LeiMgr.COLUMN, LeiMgr.ROW)

        tv_start.setOnClickListener {
            resetGame()
            lei_view.invalidate()
        }

        lei_view.addListener(object : LeiView.OnListener {
            override fun onItemClick(position: Int) {
                onClick(position)
                lei_view.invalidate()
            }

            override fun onItemLongClick(position: Int) {
                onLongClick(position)
                lei_view.invalidate()
            }
        })
    }

    /**
     * 探索、点开
     */
    private fun onClick(position: Int) {
        if (!stop) {
            when (typeList[position]) {
                TYPE_HIDE -> {
                    when (numList[position]) {
                        DEFAULT_NUM -> {
                            typeList[position] = TYPE_SHOW
                            LeiMgr.showZeroNearby(position, numList, typeList)
                        }
                        IS_LEI_NUM -> {
                            gameOver() // 点到雷，游戏结束
                            LeiMgr.gameOver(numList, typeList)
                        }
                        else -> typeList[position] = TYPE_SHOW
                    }
                }
                TYPE_SIGN -> {
                }
                TYPE_SHOW -> {
                }
            }
        }
    }

    /**
     * 标记
     */
    private fun onLongClick(position: Int) {
        if (!stop) {
            when (typeList[position]) {
                TYPE_HIDE -> {
                    typeList[position] = TYPE_SIGN
                    setLeiNum(leiNum - 1)
                }
                TYPE_SIGN -> {
                    setLeiNum(leiNum + 1)
                    typeList[position] = TYPE_HIDE
                }
                TYPE_SHOW -> {
                }
            }
        }
    }

    /** 重置游戏*/
    private fun resetGame() {
        stop = false
        typeList.forEachIndexed { index, it ->
            when (it) {
                TYPE_HIDE -> {
                }
                else -> typeList[index] = TYPE_HIDE
            }
        }
        LeiMgr.start(numList)
        setLeiNum(LeiMgr.TOTAL)

        tv_time.text = getTime()
        tv_time_end.text = getTime()
    }

    private fun getTime(): String {
        val formatter = SimpleDateFormat("HH:mm", Locale.getDefault())
        return formatter.format(Date(System.currentTimeMillis()))
    }

    private fun setLeiNum(num: Int) {
        leiNum = num
        val text: String = when (num) {
            0 -> {
                var isAllLei = true
                typeList.forEachIndexed { index, it ->
                    if (it == TYPE_SIGN && numList[index] != IS_LEI_NUM) {
                        isAllLei = false
                        return@forEachIndexed
                    }
                }
                if (isAllLei) {
                    stop = true
                    tv_time_end.text = getTime()
                    "恭喜过关!"
                } else {
                    "【还有" + num + "个雷】"
                }
            }
            else -> "【还有" + num + "个雷】"
        }
        tv_title.text = text
    }

    private fun gameOver() {
        // 震动
        val vibrator = this.getSystemService(VIBRATOR_SERVICE) as Vibrator
        vibrator.vibrate(1000)

        stop = true
        tv_title.text = "游戏结束！"
    }
}
